<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<style>
    ul {
        padding: 0;
        margin: 0;
        list-style: none;
    }

    li{
        border: 1px solid skyblue;
        margin: 10px;
        max-width: 500px;
    }
</style>

<body>
    <div id="app">
        <fieldset>
            <legend>{{isLogin?'内容界面':'登入界面'}}</legend>
            <div class="content" v-if="isLogin">
                <h1>列表展示</h1>
                <button @click="isLogin=false">点击退出登入</button>
                <button @click="getList">点击获取列表数据</button>
                <h2>
                    <img :src="userInfo.headimgurl" width="50" alt="">
                    昵称：
                    <a href="">{{userInfo.nickname}}</a>
                </h2> 
                <ul>
                    <li v-for="item in lists " :key="item._id">
                        <b>{{item.name}}</b>
                        <button v-has:read="permissionBtns">查看</button>
                        <button v-has:edit="permissionBtns">修改</button>
                        <button v-has:delete="permissionBtns">删除</button>
                        <button v-has:add="permissionBtns">增加</button>
                    </li>
                </ul>
            </div>

            <div class="login" v-else>
                <h1>请登入</h1>
                <label for="username">用户名:</label>
                <input type="text" id="username" v-model="username">
                <br>
                <label for="password">密码:</label>
                <input type="text" v-model="password" @keydown.enter="login">
                <br>
                <button @click="login">登录</button>
            </div>


         
        </fieldset>
    </div>
</body>

</html>
<script src='https://cdn.jsdelivr.net/npm/vue@2.6.14/dist/vue.js'></script>
<script>

    const loginApi = (params = {}) => {
        return fetch ('http://chst.vip/users/login',{
            method:'post',
            headers:{
                'content-type':'application/json'
            },
            body:JSON.stringify(params)
        })
        .then(body => body.json())
        .catch(err => err.tostring())
    }
    const getListApi = () =>{
        return fetch('http://chst.vip/students/getstulist',{
            method:'get',
            headers:{
                'authorization':sessionStorage.getItem('user-token')
            }
        })
        .then(body=>body.json())
        .catch(err=>err.tostring())
    }
    Vue.directive('has',{
        inserted(el,binding){
            console.log(el);
            // el.disabled = true;
            let role = binding.arg;
            let userRole = binding.value;
            let hasRole = userRole.includes(role);
            if(!hasRole){
                // el.disabled=true
                el.parentNode.removeChild(el)
            }
        } 
    })
    let vm = new Vue({
        el: '#app',
        data: {
            lists:[],
            userInfo:{},
            username:'',
            password:'',
            isLogin:false,
            todos: [
                {
                    id: 1,
                    todo: '多读书',
                    isComplete: false
                },
                {
                    id: 2,
                    todos: '多睡觉',
                    isComplete: false
                },
                {
                    id: 3,
                    todos: '多看报',
                    isComplete: false
                }
            ]
        },
        methods: {
            async login(){
            let res = await loginApi({username:this.username,password:this.password})
                console.log(res);
                if(res.state){
                    this.isLogin = true;
                    this.userInfo=res.userInfo;
                    sessionStorage.setItem('user-token',res.token)
                    this.permissionBtns = res.permission.buttons
                    this.getList()
                }else{
                    alert(res.msg)
                }
           },
           async getList(){
                let res = await getListApi()
                if(res.state){
                    this.lists=res.data
                }else{
                    alert(res.msg)
                }
           }
        }
    })
</script>